package com.zenithsun.news.newsreview.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.syzg.common.config.SysProperties;
import com.syzg.common.controller.BaseController;
import com.syzg.common.util.SearchMap;
import com.syzg.common.model.WebObj;
import com.syzg.common.exception.BaseException;
import com.syzg.core.page.PageResult;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zenithsun.common.bean.ModelType;
import com.zenithsun.common.bean.OutMessage;
import com.zenithsun.common.bean.OutMessageState;
import com.zenithsun.common.review.model.ReviewState;
import com.zenithsun.common.review.model.Reviewdetail;
import com.zenithsun.common.review.service.ReviewService;
import com.zenithsun.common.review.service.ReviewdetailService;
import com.zenithsun.common.security.sql.SqlInjectionManager;
import com.zenithsun.news.newsreview.model.NewsReview;
import com.zenithsun.news.newsreview.service.NewsReviewService;

/**
 * 新闻审核
 * @author wanglei@enersun.com.cn
 * @createTime 2016-6-12 09:56:53
 */
@Controller
@RequestMapping("/path/newsReview")
public class NewsReviewController extends BaseController{
	
	static Logger log = LoggerFactory.getLogger(NewsReviewController.class);
	
	@Resource
	private NewsReviewService newsReviewService;
	@Resource
	private ReviewdetailService reviewdetailService;
	@Resource
	private ReviewService reviewService;
	
	
	/** 
	 * 设置共用查询条件
	 * @return SearchMap
	 */
	private void setShareSearchMap(SearchMap searchMap){
		searchMap.eq("a.busMark", ModelType.News.toString());
		searchMap.createAlias("a.newsinfo", "newsinfo");
	}

	/**
	 * 未审核新闻列表
	 * @param webObj
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/reviewMain")
	public ModelAndView reviewMain()throws Exception{
		ModelAndView mv = new ModelAndView("/zenithsun/news/review/listreview.jsp");
        return mv;
	}
	
	/**
	 * 根据状态确定进入哪一个页面
	 * @param state 不允许为空
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/initlistreview")
	public ModelAndView initlistreview(String state)throws Exception{
		ModelAndView mv = new ModelAndView("/zenithsun/news/review/listreviewpass.jsp");
		if(StringUtils.isNotBlank(state)){
			if(ReviewState.WaitReview.toString().equals(state)){
				mv.setViewName("/zenithsun/news/review/listnotreview.jsp");
			}else if(ReviewState.ReviewFail.toString().equals(state)){
				mv.setViewName("/zenithsun/news/review/listreviewnopass.jsp");
			}else if(ReviewState.ReviewSuccess.toString().equals(state)){
				mv.setViewName("/zenithsun/news/review/listreviewpass.jsp");
			}
		}
        mv.addObject("state",state);
        return mv;
	}
	
	/**
	 * 通过审核状态加载新闻列表
	 * @param state
	 * @param pageNo
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value = "/listreviewjson", produces = "application/json;charset=UTF-8")
	public @ResponseBody String listreviewjson(String title,String state,int limit,int pageNo)throws Exception{
		SqlInjectionManager.validateSqlParam(new String[]{title,state});
		OutMessage outMessage = new OutMessage();
		NewsReview searchReview=new NewsReview();
		if(StringUtils.isNotBlank(state)){
			searchReview.setState(Short.parseShort(state));
		}
		searchReview.setTitle(title);
		PageResult pageResult = newsReviewService.queryReview(pageNo, limit,searchReview);//newsReviewService.queryReview(webObj.getPageNo(), SysProperties.PAGESIZE,searchReview);
		outMessage.setContent(pageResult);
		outMessage.setState(OutMessageState.Success);
		String content= JSON.toJSONString(outMessage);
		return content;
	}
	
	/**
	 * 未审核新闻列表
	 * @param webObj
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/listreview")
	public ModelAndView listreview(WebObj webObj)throws Exception{
		ModelAndView mv = new ModelAndView("/zenithsun/news/review/listreview.jsp");
		SearchMap searchMap = SearchMap.forClass(NewsReview.class);
		setShareSearchMap(searchMap);
		NewsReview searchReview=new NewsReview();
		String state=webObj.getSearchValue().get("state");
		if(StringUtils.isNotBlank(state)){
			//searchMap.eq("a.state", Short.parseShort(state));
			searchReview.setState(Short.parseShort(state));
		}
		PageResult pageResult = newsReviewService.findObjectsForPage(webObj.getPageNo(), SysProperties.PAGESIZE,searchMap);//newsReviewService.queryReview(webObj.getPageNo(), SysProperties.PAGESIZE,searchReview);
		webObj.setPageResult(pageResult);
        mv.addObject("webObj",webObj);
        return mv;
	}
	
	@RequestMapping("/viewreview")
	public ModelAndView viewreview(String revId) throws Exception {
		ModelAndView mv = new ModelAndView("/zenithsun/news/review/viewreview.jsp");
		try {
			NewsReview reviewVO = newsReviewService.findNewsReview(revId);
			mv.addObject("review",reviewVO);
			//对所有的审核明细进行排序
			List<Reviewdetail> reviewdetailList=reviewdetailService.listCurrReview(revId);
			mv.addObject("reviewdetail", reviewdetailList);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
}